function generate_jsonl_dataset(opts)
    % [buses, branches, substations, init_sw] = yantian_vdata('H:\大模型\项目\402算例\读取程序(依据方式编排数据)\');
    [buses, branches, substations, ~] = opts.data_handle(opts.data_handle_args);
    system_prompt = readlines(opts.system_prompt_file);
    system_prompt = strjoin(system_prompt, '\n');
    
    user_prompt_prototype = readlines(opts.grids_file);
    user_prompt_prototype = strjoin(user_prompt_prototype, '\n');
    
    msg(1).role = 'system';
    msg(1).content = system_prompt;
    msg(2).role = 'user';
    msg(2).content = "";
    msg(3).role = 'assistant';
    msg(3).content = "";
    
    load(opts.solution_path)
    
    Nfaults = length(sol);
    [fault_cmd, solution_cmd] = sample_fault_tiny(fault_br_name, sol, substations, buses, branches, G, initial_grid);
    
    fileID = fopen(opts.jsonl_path, 'w');
    for k = 1:Nfaults
        msg(2).content = user_prompt_prototype+"\n"+fault_cmd(k);
        msg(3).content = solution_cmd(k);
        jsonStr = jsonencode(struct('messages', msg));
        fprintf(fileID, '%s\n', jsonStr);
    end
    
    fclose(fileID);
end


function [fault_cmd, solution_cmd] = sample_fault_tiny(fault_br_name, sol_cell, subs, buses,branches, G, initial_grid)
    Nfaults = length(sol_cell);

    fault_br_name = table(fault_br_name,'VariableNames',{'name'});
    fault_buses=innerjoin(fault_br_name,branches,'Keys','name','RightVariables',{'bus1','bus2'});
    fault_cmd =  compose("The fault occurs at line between bus %s and bus %s.",fault_buses.bus1, fault_buses.bus2);

    solution_cmd = strings(Nfaults, 1);
    src_conn = subs(:,{'bus','busid'});
    loadidx=buses.Pd>0 | buses.Qd>0;
    bus_supply = buses(loadidx,{'bus','busid'});
    for k = 1:Nfaults
        % 查询动作的开关
        if isempty(sol_cell{k}) || sol_cell{k}.success ~= 1
            %无可行解的没有动作
            solution.success = false;
            solution.action = [];
            solution.supplied_loads = [];
            solution_cmd(k) = jsonencode(solution, "PrettyPrint", false);
            continue
        end

        solution.success = true;
        idx = xor(sol_cell{k}.branch_status.status, initial_grid);
        action_sw = table(G.Edges.name(idx),sol_cell{k}.branch_status.status(idx),'variableNames',{'name','status'});
        action_sw=innerjoin(action_sw,branches,'Keys','name','RightVariables',{'bus1','bus2'});
        solution.action = compose("(%s,%s,%d)", action_sw.bus1,action_sw.bus2, action_sw.status);

        % 查询每条母线从哪一条馈线得电
        idx = find(~sol_cell{k}.branch_status.status);
        H = rmedge(G, idx);
        %连通图算法
        bins = conncomp(H);
        bins = reshape(bins, [], 1);
        %连接表
        %暂时不检查双电源供电的情况
        src_conn.conn=bins(src_conn.busid);
        solution.supplied_loads=[];
        bus_supply.conn = bins(bus_supply.busid);
        % bus_supply = outerjoin(bus_supply, src_conn, 'Keys', 'conn', 'RightVariables', {'supply'});
        % bus_supply(:, 'supply') = fillmissing(bus_supply(:, 'supply'), 'constant', "offline");
        offline_idx=true(height(bus_supply),1);
        for ss = 1:height(src_conn)
            idxss=(bus_supply.conn==src_conn.conn(ss));
             offline_idx= offline_idx & ~idxss;
            solution.supplied_loads.("F"+src_conn.bus(ss)) = bus_supply.bus(idxss);
        end
        solution.supplied_loads.offline = bus_supply.bus(offline_idx);
        solution_cmd(k) = jsonencode(solution,"PrettyPrint",false);
    end

end